//
//  PayBankListView.m
//  JiFengDaRen
//
//  Created by CYF on 2017/3/2.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//
#define  KCELLHEIGHT __I6Height(50.f)
#define  KMYBGVIEWHEIGHT __I6Height(300.f)
#import "BankCardModel.h"
#import "PayBankCell.h"
#import "AddPayBankCell.h"
#import "PayBankListView.h"
@interface PayBankListView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIView *myBgView;
@property(nonatomic,strong)UIView *firstView;
@property(nonatomic,strong)UITableView *customTableView;//列表
@property(nonatomic,strong)UIButton *dissmissButton;//收起按钮
@property(nonatomic,strong)NSMutableArray *dataSourceArray;//银行卡数据源
@property(nonatomic ,strong)UILabel *payBankCardLabel;//头部标题
@property(nonatomic)BOOL  showAllBankCard;//头部标题


@end

@implementation PayBankListView

- (instancetype)initWithFrame:(CGRect)frame usingChoices:(NSNumber *)usingChoices bankCardType:(int )bankCardType{
     return  [self initWithFrame:frame usingChoices:usingChoices bankCardType:bankCardType bankCardId:nil showAllBankCard:NO];
}
// 初始化
- (instancetype)initWithFrame:(CGRect)frame usingChoices:(NSNumber *)usingChoices bankCardType:(int )bankCardType showAllBankCard:(BOOL)showAllBankCard {
    
    return  [self initWithFrame:frame usingChoices:usingChoices bankCardType:bankCardType bankCardId:nil showAllBankCard:showAllBankCard];
}

- (instancetype)initWithFrame:(CGRect)frame usingChoices:(NSNumber *)usingChoices bankCardType:(int )bankCardType bankCardId:(NSString *)bankCardId showAllBankCard:(BOOL)showAllBankCard{
    
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
        self.using_choices = usingChoices;
        self.showAllBankCard =  showAllBankCard;
        [self getPayBankCardListBankCardType:bankCardType bankCardId:bankCardId];
        
       
    }
    return  self;
    
}

- (void)getPayBankCardListBankCardType:(int )bankCardType bankCardId:(NSString *)bankCardId{
    

     
        [SHUserCenterHttp getBankCardLsitWithUsingChoices:[self.using_choices intValue] bankCardType:bankCardType  success:^(NSMutableArray *bankCardListArray) {
            [MBProgressHUD  hideHUDForView:self animated:YES];
            
            for (BankCardModel *model in bankCardListArray) {
                if (!bankCardId ) {
                    model.using_choices = [self.using_choices  intValue];
                    if (model.auth_4_status) {
                        model.bank_card_type = bankCardType;
                        [self.dataSourceArray insertObject:model atIndex:0];
                    }
                }else{
                    model.using_choices = [self.using_choices  intValue];
                    if (model.auth_4_status && (model._id != [bankCardId intValue])){
                        model.bank_card_type = bankCardType;
                        [self.dataSourceArray insertObject:model atIndex:0];
                    }
                }
            }
            if (!self.showAllBankCard) {
                BankCardModel *model = [[BankCardModel alloc]init];
                model.noBankCard = 1;
                [self.dataSourceArray addObject:model];
            }
            [self.customTableView reloadData];
            
        } complection:^(id result) {
            [MBProgressHUD  hideHUDForView:self animated:YES];
            
        } failure:^(id erro) {
            [MBProgressHUD  hideHUDForView:self animated:YES];
            
        }];
        
        if (self.showAllBankCard) {
            
            [SHUserCenterHttp getBankCardLsitWithUsingChoices:1 bankCardType:11  success:^(NSMutableArray *bankCardListArray) {
                for (BankCardModel *model in bankCardListArray) {
                    if (!bankCardId ) {
                        model.using_choices = [self.using_choices  intValue];
                        if (model.auth_4_status) {
                            model.bank_card_type = 11;
                            [self.dataSourceArray addObject:model];
                        }
                    }else{
                        model.using_choices = [self.using_choices  intValue];
                        if (model.auth_4_status && (model._id != [bankCardId intValue])){
                            model.bank_card_type = 11;
                            [self.dataSourceArray addObject:model];
                            
                        }
                    }
                    
                }
                
                BankCardModel *model = [[BankCardModel alloc]init];
                model.noBankCard = 1;
                [self.dataSourceArray addObject:model];
                
                [self.customTableView reloadData];
                
                
                [self.customTableView reloadData];
                
            } complection:^(id result) {
                [MBProgressHUD  hideHUDForView:self animated:YES];
                
            } failure:^(id erro) {
                [MBProgressHUD  hideHUDForView:self animated:YES];
                
            }];
        }
    
}

- (NSMutableArray *)dataSourceArray{
    
    if (!_dataSourceArray) {
        _dataSourceArray = [[NSMutableArray alloc]init];
        
    }
    return _dataSourceArray;
}
- (UIView *)myBgView{
    
    if (!_myBgView) {
        _myBgView = [[UIView alloc]initWithFrame:CGRectMake(0,KScreenHeight, KScreenWidth, KScreenHeight)];
        _myBgView.backgroundColor = KBASEBGCOLOR;
        [self addSubview:_myBgView];
        
    }
    
    return _myBgView;
}

- (UIView *)firstView{
    if (!_firstView) {
        _firstView = [[UIView alloc]initWithFrame:CGRectMake(0,0, KScreenWidth,__I6Height(60))];
        _firstView.backgroundColor = [UIColor whiteColor];
        [self.myBgView addSubview:_firstView];

    }
    return _firstView;
}
//懒加载 支付类型列表
- (UITableView *)customTableView{
    
    if (!_customTableView) {
        _customTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _customTableView.showsVerticalScrollIndicator = NO;
        _customTableView.delegate = self;
        _customTableView.dataSource = self;
        _customTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.myBgView addSubview:_customTableView];
        [_customTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.myBgView.mas_bottom).with.offset(0);
            make.width.mas_equalTo(KScreenWidth);
            make.centerX.mas_equalTo(self.mas_centerX);
            make.top.mas_equalTo(self.firstView.mas_bottom).with.offset(0);
        }];
        
        [self.customTableView registerClass:[PayBankCell class] forCellReuseIdentifier:@"bankcard"];
        [self.customTableView registerClass:[AddPayBankCell class] forCellReuseIdentifier:@"addbank"];

    }
    return  _customTableView;
}

//懒加载 取消按钮
- (UIButton *)dissmissButton{
    
    if (!_dissmissButton) {
        _dissmissButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_dissmissButton setImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
        [_dissmissButton setTitleColor:KTEXTCOLOR  forState:UIControlStateNormal];
        _dissmissButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_dissmissButton addTarget:self action:@selector(dissMissCustomView) forControlEvents:UIControlEventTouchUpInside];
        [self.firstView addSubview:_dissmissButton];
        
        [_dissmissButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.firstView.mas_right).with.offset(0);
            make.centerY.mas_equalTo(self.firstView.mas_centerY);
            make.width.mas_equalTo(__I6Width(50));
            make.height.mas_equalTo(__I6Width(50));
            
        }];
    }
    return  _dissmissButton;
}
- (UILabel *)payBankCardLabel{
    
    if (!_payBankCardLabel) {
        _payBankCardLabel = [[UILabel alloc]init];
        _payBankCardLabel.text = @"";
        _payBankCardLabel.textColor = KTEXTCOLOR;
        _payBankCardLabel.font = KSystemFont(__I6Width(16));
        _payBankCardLabel.textAlignment = NSTextAlignmentCenter;
        [self.firstView addSubview:_payBankCardLabel];
        [_payBankCardLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.firstView.mas_left).with.offset(__I6Width(10));
            make.right.mas_equalTo(self.mas_right).with.offset(__I6Width(0));
            make.centerY.mas_equalTo(self.firstView.mas_centerY);
            make.height.mas_equalTo(__I6Width(20));
            
        }];

    }
    return _payBankCardLabel;
}
// 添加视图
- (void)addSubViews{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self myBgView];
    [self firstView];
    [self dissmissButton];
    [self payBankCardLabel];
    [self customTableView];
    if ([self.using_choices isEqualToNumber:@(1)]) {//添加收款卡
        
        _payBankCardLabel.text = @"选择收款银行卡";
        
    }else{
        _payBankCardLabel.text = @"请选择付款银行卡";
        
    }

}

//显示视图
- (void)showCustomView{
    [self addSubViews];
    CGRect frame = self.myBgView.frame;
    frame =  CGRectMake(0,KScreenHeight - KMYBGVIEWHEIGHT, KScreenWidth, KMYBGVIEWHEIGHT);
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.myBgView.frame = frame;
    } completion:^(BOOL finished) {
        
        
    }];
    
}

//隐藏视图
- (void)dissMissCustomView{
    [self.customTableView removeFromSuperview];
    [self.dissmissButton removeFromSuperview];
    [self.myBgView removeFromSuperview];
    [self removeFromSuperview];
    
}



#pragma mark ---- UITableView Delegate & Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return  self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PayBankCell *payBankCell;
    AddPayBankCell *addPayBankCell;
    BankCardModel *model = self.dataSourceArray[indexPath.row];
    if (model.noBankCard) {
       
        addPayBankCell = [tableView dequeueReusableCellWithIdentifier:@"addbank"];
        if ([self.using_choices isEqualToNumber:@(1)]) {//添加收款卡
            
            addPayBankCell.nameLabel.text = @"请添加收款银行卡";
            _payBankCardLabel.text = @"选择收款银行卡";
            
        }else{
            _payBankCardLabel.text = @"请选择付款银行卡";
            addPayBankCell.nameLabel.text = @"添加付款银行卡";

        }

        return addPayBankCell;
        
    }else{
        payBankCell = [tableView dequeueReusableCellWithIdentifier:@"bankcard"];
        payBankCell.model = model;
        return payBankCell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return KCELLHEIGHT;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self dissMissCustomView];
    BankCardModel *model = self.dataSourceArray[indexPath.row];
    if (model.noBankCard) {
        [_delegate payBankListView:self bankCardCode:@"0" bankCardId:@"0"  usingChoices:self.using_choices];

    }else{
        if ([_delegate respondsToSelector:@selector(payBankListView:bankCardCode: bankCardId: usingChoices:)]) {
            [_delegate payBankListView:self bankCardCode:model.bank_card_code  bankCardId:@(model._id)  usingChoices:self.using_choices];
        }
    }
   
}



@end
